

/**
 * article
 {
     _id:ObjectId,
     title:string,
     content:string,
     keys:[string],
     date:[timestamp]
     userId:ObjectId,
 }

 */
/**
 * comment
 {
     _id:ObjectId,
     articleId:ObjectId,
     userId:ObjectId,
     pId:ObjectId,
     content:string,
     date:[timestamp]
 }
 用户表的结构
 user{
    _id:ObjectId,
    username:string,
    nickname:string,
    password:string,
    email:string,
    state:int
}
 *
 */
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var url = 'mongodb://localhost/test';
mongoose.connect(url);
var conn = mongoose.connection;


var article = new Schema({
    title:  String,
    content:   String,
    keys:{type:[String],set:formatKeys,get:getKeys},
    date: { type: Date, default: Date.now },
    userId:Schema.Types.ObjectId
});
var comment = new Schema({
    articleId:Schema.Types.ObjectId,
    userId:Schema.Types.ObjectId,
    pId:Schema.Types.ObjectId,
    content:String,
    date:{type:Date,default:Date.now}
});
var user = new Schema({
    username:String,
    nickname:String,
    password:String,
    email:String,
    state:Number
});
function formatKeys(keys){
    console.log(keys);
    if(typeof keys === 'string'){
        console.log(keys.split(/,/));
        return keys.split(/,/);
    }else{
        return keys;
    }
}
function getKeys(keys){
    var str;
    if(Array.isArray(keys)){
        str = keys.reduce(function(p,c,i){
            if(i===0)
                return c;
            else
                return p+','+c;
        },'');
    }else{
        str = keys;
    }

    return str;
}
var Article = mongoose.model('article',article);
var Comment = mongoose.model('comment',comment);
var User = mongoose.model('user',user);
+function(){
    //建立用户
    //var u = new User({username:'dongge',nickname:'xx',password:'123456',email:'yuzhou346694246@126.com',state:1});
    //u.save(function(err){
    //    console.log(u.id);
    // });
    //查找用户568fec308210d5481724dd45
    //User.findOne({username:'dongge'},function(err,u){
    //    console.log(u);
    //})
    //删除用户
    //User.remove({_id:'568fec308210d5481724dd45'},function(err){
    //    console.log(err);
    //})
    //编辑用户
    //User.update({_id:'568ff3c5dba46b041225cffe'},{$set:{username:'root'}},function(err){
    //    console.log(err);
    //});
    //登录
    //User.findOne({username:'root',password:'123456'},function(err,u){
    //    console.log(u);
    //})
}();

+function(){
    //新建文章
    //var a = new Article({title:'dongge',content:'this is a test',keys:['xxx','jquery'],userId:'568fec308210d5481724dd45'});
    //a.save(function(err,d){
    //    console.log(d);
    //    console.log(a);
    //})
    //查找文章568ff9879d79a91424171734
    Article.findById('568ff9879d79a91424171734').then(function(a){
        //if(a.userId)
        console.log(a);
        console.log(a.keys);
        return User.findById(a.userId);

    }).then(function(u){
        console.log(u);
    });
    //修改
    //Article.update({_id:'568ff9879d79a91424171734'},{$set:{userId:'568ff3c5dba46b041225cffe'}},function(err,n){
    //    console.log(err);
    //    console.log(n);
    //})


}();
/*+function(){
    Article.findById('568fbfaf0cbcae5c18bca741',function(err,doc){
        console.log(doc);
    })
}();*/
